﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace WebRequest.WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            // 初始化 WebBrowser 的 HTML 内容
            webBrowser1.DocumentText = @"
                <html>
                    <body style='font-family:Segoe UI; font-size:14px;'>
                        <div id='output'></div>
                    </body>
                </html>";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var url = "http://ud.hxhis.al.sunwaysaga.com/Files%20Cache/Downloads/InternetHospitalWinform.DX.2025.2.21.917.exe";
            //var url = "http://localhost:48275/Home/TestFile";
            var request = (HttpWebRequest)System.Net.WebRequest.Create(url);
            // Method 属性可以设置为任意 HTTP 1.1 协议谓词：GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。
            request.Method = "HEAD";
            using (var response = request.GetResponse())
            {
                response.Close();
                foreach (string responseHeader in response.Headers)
                {
                    richTextBox1.AppendText($"{responseHeader}:{response.Headers[responseHeader]}{Environment.NewLine}");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            webBrowser1.DocumentText = @"
                <html>
                    <body style='font-family:Segoe UI; font-size:14px;'>
                        <div id='output'></div>
                    </body>
                </html>";
            var url = "https://gw.app.bjsdxqrmyy.cn/hisApi/api/services/LisRapports/V1_0_0_0/GetDeepSeekInfoByString";
            //var url = "http://localhost:48275/Home/TestFile";
            if (!string.IsNullOrWhiteSpace(textBox1.Text))
            {
                url = textBox1.Text;
            }
            var request = (HttpWebRequest)System.Net.WebRequest.Create(url);
            // Method 属性可以设置为任意 HTTP 1.1 协议谓词：GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。
            request.Method = "POST";
            var selected = flowLayoutPanel2.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);
            if (selected != null)
            {
                request.Method = selected.Text;
            }

            request.ContentType = "application/json";
            if (!string.IsNullOrWhiteSpace(textBox3.Text))
            {
                request.Headers.Set(HttpRequestHeader.Authorization, $"Bearer {textBox3.Text}");
            }
            if (request.Method == "POST")
            {
                using (var rws = request.GetRequestStream())
                {
                    //var input = "{\"Number\": \"2025-06-10|131|1|369\"}";
                    var input = string.Empty;
                    if (!string.IsNullOrWhiteSpace(textBox2.Text))
                    {
                        input = textBox2.Text;
                    }
                    var bytes = Encoding.UTF8.GetBytes(input);
                    rws.Write(bytes, 0, bytes.Length);
                    rws.Flush();
                    rws.Close();
                }
            }

            using (var response = request.GetResponse())
            {
                using (var rrs = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(rrs, Encoding.UTF8))
                    {
                        int charset;
                        while ((charset = sr.Read()) != -1)
                        {
                            richTextBox1.EndInvoke(richTextBox1.BeginInvoke(new Action<int>(cs =>
                            {
                                richTextBox1.AppendText(((char)cs).ToString());
                                Application.DoEvents();
                            }), charset));

                            webBrowser1.EndInvoke(webBrowser1.BeginInvoke(new Action<int>(cs =>
                            {
                                // 获取目标 div 元素
                                var doc = webBrowser1.Document;
                                if (doc != null)
                                {
                                    var outputDiv = doc.GetElementById("output");

                                    if (outputDiv != null)
                                    {
                                        outputDiv.InnerText += (char)cs; // 更新 HTML 内容
                                    }
                                }

                                Application.DoEvents();
                            }), charset));
                        }
                    }
                }

                var doc1 = webBrowser1.Document;
                if (doc1 != null)
                {
                    var outputDiv = doc1.GetElementById("output");

                    if (outputDiv != null)
                    {
                        outputDiv.InnerHtml = outputDiv.InnerText;
                    }
                }
                richTextBox1.AppendText(Environment.NewLine);
                foreach (string responseHeader in response.Headers)
                {
                    richTextBox1.AppendText($"{responseHeader}:{response.Headers[responseHeader]}{Environment.NewLine}");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Clear();
            //var url = "https://gw.app.bjsdxqrmyy.cn/hisApi/api/services/LisRapports/V1_0_0_0/GetDeepSeekInfoByString";
            var url = "http://localhost:48275/api/Steam";
            var request = (HttpWebRequest)System.Net.WebRequest.Create(url);
            // Method 属性可以设置为任意 HTTP 1.1 协议谓词：GET、HEAD、POST、PUT、DELETE、TRACE 或 OPTIONS。
            request.Method = "GET";
            //request.ContentType = "application/json";
            //using (var rws = request.GetRequestStream())
            //{
            //    var input = "{\"Number\": \"2025-06-10|131|1|369\"}";
            //    var bytes = Encoding.UTF8.GetBytes(input);
            //    rws.Write(bytes, 0, bytes.Length);
            //    rws.Flush();
            //    rws.Close();
            //}

            using (var response = request.GetResponse())
            {
                using (var rrs = response.GetResponseStream())
                {
                    using (var sr = new StreamReader(rrs, Encoding.UTF8))
                    {
                        int charset;
                        while ((charset = sr.Read()) != -1)
                        {
                            richTextBox1.EndInvoke(richTextBox1.BeginInvoke(new Action<int>(cs =>
                            {
                                richTextBox1.AppendText(((char)cs).ToString());
                                Application.DoEvents();
                            }), charset));
                        }
                    }
                }

                foreach (string responseHeader in response.Headers)
                {
                    richTextBox1.AppendText($"{responseHeader}:{response.Headers[responseHeader]}{Environment.NewLine}");
                }
            }
        }
    }
}
